<template>
    <el-cascader
    :value="value"
    :options="treeData"
    :props="props"
    size="mini"
    separator="-"
    @change="handleChange"
    ></el-cascader>
</template>

<script>
import {getDepartmentList} from '@/api/department'
import {transListToTree} from '@/utils/index'
export default {
    data() {
        return {
            treeData: [],
            props: {
                value: 'id',
                label: 'name',
                expandTrigger: 'hover'
            }
        }
    },
    methods: {
        // 获取部门信息
        async getData() {
            const res = await getDepartmentList()
            this.treeData = transListToTree(res, 0)
            console.log(this.treeData);
        },
        handleChange(data) {
           if (data.length >0) {
            //    触发input事件
            this.$emit('input', data[data.length - 1])
           }
           else {
            this.$emit('input', null)
           }
        }
    },
    created() {
        this.getData()
    },
    props: {
        value: {
            type: Number,
            default: null
        }
    }
}
</script>